#include "sigslottestpage.h"
#include <QVBoxLayout>
#include <QDebug>

SigSlotTestPage::SigSlotTestPage(QWidget *parent) : QWidget(parent)
{
    SigSlotTest *receiver = new SigSlotTest(this);
    receiver->setObjectName(tr("receiver"));

    m_senders.clear();
    SigSlotTest *sender;
    for (int i = 0; i < 100; i++)
    {
        sender = new SigSlotTest(this);
        sender->setObjectName(tr("sender_%1").arg(i));

        // 各级sender连接起来
        if (m_senders.size())
            connect(m_senders.last(), &SigSlotTest::sig, sender, &SigSlotTest::onSig);

        m_senders << sender;
    }
    // 第一个和最后一个sender连接到receiver
    connect(m_senders.first(), &SigSlotTest::sig2, receiver, &SigSlotTest::onSig);
    connect(m_senders.last(), &SigSlotTest::sig, receiver, &SigSlotTest::onSig);

    m_timer = new QTimer(this);
    connect(m_timer, &QTimer::timeout, this, [=](){
        emit m_pbSend->clicked();
    });
//    m_timer->start(1);

    m_pbSend = new QPushButton(tr("Send"));
    connect(m_pbSend, &QPushButton::clicked, this, [=](){
        static int cnt = 0;

        qDebug() << "emit sig...";
        emit m_senders.first()->sig(cnt++);
        qDebug() << "emit sig----end";

        qDebug() << "emit sig2...";
        emit m_senders.first()->sig2(cnt++);
        qDebug() << "emit sig2----end";
    });

    QVBoxLayout *vbl = new QVBoxLayout();
    vbl->addWidget(m_pbSend);
    setLayout(vbl);
}
